﻿/*
	Copyright 2009 CATsoft Development GmbH

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

/*
Animation Functions
*/
	


function animate(){

	var lbxMainItems;  //numer of items currently in main listbox
	var albumArtAnimation = null;
	var playingAnimation;
	var rotateTimer;
	var progressAnimation;


/**************************************************************
	rotate icon functions while httprequest
*************************************************************/
	animate.prototype.stopLodingAnimation = function(){
		clearInterval(this.rotateTimer);
		imgLoading.visible = false;
	}

	animate.prototype.startLodingAnimation = function(){
		imgLoading.visible = true;
		imgLoading.rotation = 0;
		clearInterval(this.rotateTimer);
		this.rotateTimer = view.setInterval(function(){ rotateIcon(imgLoading); }, 1);
	}



/**************************************************************
	set opacity of background
*************************************************************/
	animate.prototype.setOpacity = function(opVal){
		bg.opacity = opVal;
	}

/**************************************************************
	change backgroundcolor and fontcolor
*************************************************************/
	animate.prototype.setTextcolor = function(textcol){
		lblCatsoftTitle.color = textcol;
		lineTop.background = lineBottom.background = lineLeft.background = lineMiddle.background = lineRight.background = textcol;
		lblAlbum.color = lblArtist.color = lblTrack.color = lblPlaylist.color = textcol;
		lblInfoText.color = textcol;
		lblBack.color = textcol;
	}

	animate.prototype.changeBgcolor = function(redVal, greenVal, blueVal){
		var bgcol = "#" + intToHex(redVal) + intToHex(greenVal) + intToHex(blueVal);

		// change fontcolor when background gets too pale
		if(redVal +greenVal + blueVal > 400){
			this.setTextcolor("#000000");
		} else {
			this.setTextcolor("#FFFFFF");
		}

		bg.background = bgcol;
		dvMain.background = bgcol;
}

/**************************************************************
	make cover invisible when mouseover
*************************************************************/
	animate.prototype.mouseOverCover = function(){
		hideAlbumArt(3000);
	}


/**************************************************************
	fade cover back
*************************************************************/
	animate.prototype.showCoveragain = function(){
		if(imgAlbumcover.visible == false){
			imgAlbumcover.opacity = 0;
			imgAlbumcover.visible = true;
			this.albumArtAnimation = view.beginAnimation(function() { setElementOpacity(imgAlbumcover); }, 0, 255, 15000);
		}
	}



/**************************************************************
	updates progressbar
*************************************************************/
 animate.prototype.showTrackStatus = function(element){
		element.value = music.curAudioClip.currentPosition;
	}

/**************************************************************
	scrolling functions
*************************************************************/
	animate.prototype.lbxScroll = function(){
		lbxMain.y = 0;
		lbxMain.x = 0;
		sbVertical.max = 0;
		sbVertical.value = 0;
		sbHorizontal.value = 0;
		var h = (this.lbxMainItems+1)*lbxMain.itemHeight;
	
		if(h+15>dvMain.height){
			lbxMain.height = h;
			sbVertical.max = lbxMain.height+15 - dvMain.height;
		}
	}

	animate.prototype.verticalScroll = function(){
		lbxMain.y = -sbVertical.value;
	}

	animate.prototype.horizontalScroll = function(){
		lbxMain.x = -sbHorizontal.value;
	}

/**************************************************************
	selected Track should always be visible
*************************************************************/
	animate.prototype.adjustMaintoTrack = function(currTrack, mainElement, mainItems, screenBox, vertScroll){
		var trackEntryPosition = currTrack * mainElement.itemHeight;
		mainElement.selectedIndex = currTrack;
		var isVisible = this.listEntryVisible(currTrack, mainElement, screenBox.height - vertScroll.width);
		if( !isVisible ){
			if((mainItems - currTrack) * mainElement.itemHeight > screenBox.height - vertScroll.width){
				mainElement.y = -trackEntryPosition;
				vertScroll.value = trackEntryPosition;
			} else {
				mainElement.y = screenBox + vertScroll.width - mainElement.height;
				vertScroll.value = sbVertical.max;
			}
		}
	}

/**************************************************************
	is entry visible ? returns boolean
*************************************************************/
	animate.prototype.listEntryVisible = function(entryNo, listb, screenHeight){
		var entryPosition = entryNo*listb.itemHeight;
		if(listb.y + entryPosition > screenHeight || listb.y + entryPosition < 0) {
			return false;
		} else {
			return true;
		}
	}
}

/**************************************************************
	rotate a image
*************************************************************/
function rotateIcon(img) {
    img.rotation = img.rotation + 10;
    if (img.rotation >= 720)
        img.rotation = 0;
}

/**************************************************************
	set opacity of an element
*************************************************************/
function setElementOpacity(element) {
	element.opacity = event.value;
}		

/**************************************************************
	hide album art 
	reappearTime == 0 -> no reappearing
*************************************************************/
function hideAlbumArt(reappearTime){
	if(anim.albumArtAnimation != null) {
		view.cancelAnimation(anim.albumArtAnimation);
		anim.albumArtAnimation = null;
	}
	imgAlbumcover.visible = false;
	if(reappearTime != 0){
		view.setTimeout("anim.showCoveragain()", reappearTime);
	}
}

